<HTML>
<HEAD>
<TITLE>
BeanConfig.cpp
</TITLE>
</HEAD>
<BODY>
<PRE>
<font color="green">/*
* Copyright 2006 the original author or authors.
* 
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* 
*      http://www.apache.org/licenses/LICENSE-2.0
* 
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/</font>

<font color="blue">#include</font> <font color="maroon">&#60;memory&#62;</font>
<font color="blue">#include</font> <font color="maroon">"BeanConfig.h"</font>
<font color="blue">#include</font> <font color="maroon">"IBeanWrapper.h"</font>
<font color="blue">#include</font> <font color="maroon">"AutumnException.h"</font>

<font color="blue">namespace</font> Autumn<font color="black">{</font>

<font color="green">/** erase space char */</font>
string trimString<font color="black">(</font><font color="blue">const</font> string v<font color="black">)</font>
<font color="black">{</font>
    string s<font color="black">=</font>v;
    <font color="blue">for</font><font color="black">(</font><font color="blue">int</font> i<font color="black">=</font><font color="maroon">0</font>; i<font color="black">&#60;</font>s.size<font color="black">(</font><font color="black">)</font>; <font color="black">)</font><font color="black">{</font>
        <font color="blue">if</font><font color="black">(</font> isspace<font color="black">(</font>s<font color="black">[</font>i<font color="black">]</font><font color="black">)</font> <font color="black">)</font><font color="black">{</font>
            s.erase<font color="black">(</font>i,<font color="maroon">1</font><font color="black">)</font>;
        <font color="black">}</font>
        <font color="blue">else</font> i<font color="black">+</font><font color="black">+</font>;
    <font color="black">}</font>
    <font color="blue">return</font> s;
<font color="black">}</font>


BeanConfig<font color="black">:</font><font color="black">:</font>BeanConfig<font color="black">(</font>TBean<font color="black">&</font> beandef, WrapperMaker<font color="black">*</font> wc, WrapperFreer<font color="black">*</font> wd<font color="black">)</font>
<font color="black">{</font>
    <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>Name <font color="black">=</font> beandef.Name;
    <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>WrapperCreater <font color="black">=</font> wc;
    <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>WrapperDeleter <font color="black">=</font> wd;
    <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>DependedObjects <font color="black">=</font> beandef.DependedObjects;
    <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>Singleton <font color="black">=</font> beandef.Singleton;
    <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>FactoryBean <font color="black">=</font> beandef.FactoryBean;
    <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>ConMethod <font color="black">=</font> beandef.ConMethod;
    <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>InitMethod <font color="black">=</font> beandef.InitMethod;
    <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>DestroyMethod <font color="black">=</font> beandef.DestroyMethod;
    <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>DeleteMethod <font color="black">=</font> beandef.DeleteMethod;
    <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>GotArgTypes <font color="black">=</font> <font color="blue">false</font>;
    <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>ConArgs <font color="black">=</font> <font color="blue">new</font> PropertyList;
    <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>Properties <font color="black">=</font> <font color="blue">new</font> PropertyList;
    
    <font color="blue">int</font> i;
    TProperty <font color="black">*</font>p;
    
    <font color="blue">for</font><font color="black">(</font>i<font color="black">=</font><font color="maroon">0</font>; i<font color="black">&#60;</font>beandef.ConArgs.size<font color="black">(</font><font color="black">)</font>; i<font color="black">+</font><font color="black">+</font><font color="black">)</font><font color="black">{</font>
        p <font color="black">=</font> <font color="black">&</font>beandef.ConArgs<font color="black">[</font>i<font color="black">]</font>;
        <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>ConArgs<font color="black">-</font><font color="black">&#62;</font>push_back<font color="black">(</font><font color="blue">new</font> BeanProperty<font color="black">(</font>p<font color="black">-</font><font color="black">&#62;</font>Name,
            trimString<font color="black">(</font>p<font color="black">-</font><font color="black">&#62;</font>Type<font color="black">)</font>,
            p<font color="black">-</font><font color="black">&#62;</font>Value, p<font color="black">-</font><font color="black">&#62;</font>IsBeanRef, p<font color="black">-</font><font color="black">&#62;</font>Managed<font color="black">)</font><font color="black">)</font>;
    <font color="black">}</font>
    
    <font color="green">// the following depends on BeanWrapperMacro implementation.</font>
    auto_ptr<font color="black">&#60;</font>IBeanWrapper<font color="black">&#62;</font> pw<font color="black">(</font> wc<font color="black">(</font><font color="black">)</font> <font color="black">)</font>;
    <font color="blue">for</font><font color="black">(</font>i<font color="black">=</font><font color="maroon">0</font>; i<font color="black">&#60;</font>beandef.Properties.size<font color="black">(</font><font color="black">)</font>; i<font color="black">+</font><font color="black">+</font><font color="black">)</font><font color="black">{</font>
        p <font color="black">=</font> <font color="black">&</font>beandef.Properties<font color="black">[</font>i<font color="black">]</font>;

        string tmpType <font color="black">=</font> p<font color="black">-</font><font color="black">&#62;</font>Type;
        <font color="blue">if</font><font color="black">(</font> tmpType.empty<font color="black">(</font><font color="black">)</font> <font color="black">)</font>
            pw<font color="black">-</font><font color="black">&#62;</font>getBeanPropertyType<font color="black">(</font>p<font color="black">-</font><font color="black">&#62;</font>Name.c_str<font color="black">(</font><font color="black">)</font>, tmpType<font color="black">)</font>;

        <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>Properties<font color="black">-</font><font color="black">&#62;</font>push_back<font color="black">(</font><font color="blue">new</font> BeanProperty<font color="black">(</font>p<font color="black">-</font><font color="black">&#62;</font>Name, 
                trimString<font color="black">(</font>tmpType<font color="black">)</font>, 
                p<font color="black">-</font><font color="black">&#62;</font>Value, p<font color="black">-</font><font color="black">&#62;</font>IsBeanRef, p<font color="black">-</font><font color="black">&#62;</font>Managed<font color="black">)</font><font color="black">)</font>;
    <font color="black">}</font>
<font color="black">}</font>

BeanConfig<font color="black">:</font><font color="black">:</font>~BeanConfig<font color="black">(</font><font color="black">)</font>
<font color="black">{</font>
    <font color="blue">int</font> i;
    <font color="blue">for</font><font color="black">(</font>i<font color="black">=</font><font color="maroon">0</font>; i<font color="black">&#60;</font><font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>ConArgs<font color="black">-</font><font color="black">&#62;</font>size<font color="black">(</font><font color="black">)</font>; i<font color="black">+</font><font color="black">+</font><font color="black">)</font><font color="black">{</font>
        <font color="blue">delete</font> <font color="black">(</font><font color="black">*</font><font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>ConArgs<font color="black">)</font><font color="black">[</font>i<font color="black">]</font>;
    <font color="black">}</font>
    <font color="blue">for</font><font color="black">(</font>i<font color="black">=</font><font color="maroon">0</font>; i<font color="black">&#60;</font><font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>Properties<font color="black">-</font><font color="black">&#62;</font>size<font color="black">(</font><font color="black">)</font>; i<font color="black">+</font><font color="black">+</font><font color="black">)</font><font color="black">{</font>
        <font color="blue">delete</font> <font color="black">(</font><font color="black">*</font><font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>Properties<font color="black">)</font><font color="black">[</font>i<font color="black">]</font>;
    <font color="black">}</font>
    <font color="blue">delete</font> <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>ConArgs;
    <font color="blue">delete</font> <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>Properties;    
<font color="black">}</font>

IBeanWrapper<font color="black">*</font> BeanConfig<font color="black">:</font><font color="black">:</font>createWrapper<font color="black">(</font><font color="black">)</font> <font color="blue">const</font>
<font color="black">{</font>
    IBeanWrapper<font color="black">*</font> p <font color="black">=</font> <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>WrapperCreater<font color="black">(</font><font color="black">)</font>;
    p<font color="black">-</font><font color="black">&#62;</font>setWrapperDeleter<font color="black">(</font><font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>WrapperDeleter<font color="black">)</font>;
        
    p<font color="black">-</font><font color="black">&#62;</font>setSingleton<font color="black">(</font> <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>Singleton <font color="black">)</font>;
    p<font color="black">-</font><font color="black">&#62;</font>setInitMethod<font color="black">(</font> <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>InitMethod <font color="black">)</font>;
    p<font color="black">-</font><font color="black">&#62;</font>setDestroyMethod<font color="black">(</font> <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>DestroyMethod <font color="black">)</font>;
    p<font color="black">-</font><font color="black">&#62;</font>setDeleteMethod<font color="black">(</font> <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>DeleteMethod <font color="black">)</font>;

    <font color="blue">return</font> p;
<font color="black">}</font>

PropertyList<font color="black">*</font> BeanConfig<font color="black">:</font><font color="black">:</font>getConArgs<font color="black">(</font>IBeanWrapper<font color="black">*</font> pw<font color="black">)</font> <font color="black">{</font>
    <font color="blue">int</font> argNum <font color="black">=</font> <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>ConArgs<font color="black">-</font><font color="black">&#62;</font>size<font color="black">(</font><font color="black">)</font>;

    <font color="blue">if</font><font color="black">(</font> <font color="black">!</font><font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>GotArgTypes <font color="black">&</font><font color="black">&</font> argNum <font color="black">&#62;</font> <font color="maroon">0</font> <font color="black">)</font><font color="black">{</font>
        string conMethod <font color="black">=</font> <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>getConMethod<font color="black">(</font><font color="black">)</font>;
        string ConArgTypes <font color="black">=</font> pw<font color="black">-</font><font color="black">&#62;</font>getConArgTypes<font color="black">(</font>conMethod, argNum<font color="black">)</font>;
        
        string tmpType;
        <font color="blue">int</font> start, end <font color="black">=</font> <font color="maroon">-1</font>;
        <font color="blue">for</font><font color="black">(</font><font color="blue">int</font> i<font color="black">=</font><font color="maroon">0</font>; i<font color="black">&#60;</font><font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>ConArgs<font color="black">-</font><font color="black">&#62;</font>size<font color="black">(</font><font color="black">)</font>; i<font color="black">+</font><font color="black">+</font><font color="black">)</font><font color="black">{</font>
            start <font color="black">=</font> end <font color="black">+</font> <font color="maroon">1</font>;
            end <font color="black">=</font> ConArgTypes.find<font color="black">(</font>typeSeparator, start<font color="black">)</font>;
            <font color="blue">if</font><font color="black">(</font> end <font color="black">=</font><font color="black">=</font> string<font color="black">:</font><font color="black">:</font>npos <font color="black">)</font><font color="black">{</font>
                <font color="blue">throw</font> MissDefinitionEx<font color="black">(</font><font color="maroon">"BeanConfig"</font>, <font color="maroon">"getConArgs"</font>, 
                    <font color="maroon">"the ConArgs' types are less than arguments!"</font><font color="black">)</font>;
            <font color="black">}</font>
            tmpType <font color="black">=</font> ConArgTypes.substr<font color="black">(</font>start, end <font color="black">-</font>start<font color="black">)</font>;
            
            <font color="black">(</font><font color="black">*</font><font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>ConArgs<font color="black">)</font><font color="black">[</font>i<font color="black">]</font><font color="black">-</font><font color="black">&#62;</font>setType<font color="black">(</font>trimString<font color="black">(</font>tmpType<font color="black">)</font><font color="black">)</font>;
        <font color="black">}</font>
        
        <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>GotArgTypes <font color="black">=</font> <font color="blue">true</font>;
    <font color="black">}</font>
    <font color="blue">return</font> <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>ConArgs;
<font color="black">}</font>

<font color="black">}</font> <font color="green">// End namespace Autumn</font>


</PRE>
</BODY>
</HTML>
